package com.itheima.web.controller;

import cn.hutool.core.util.NumberUtil;
import com.itheima.service.mongo.MovementService;
import com.itheima.web.manager.UserManager;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;


    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize){

        return userManager.findByPage(pageNum,pageSize);
    }

    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId){
        return userManager.findUserInfoById(userId);
    }

    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByCondition4Page(
            Long uid,
            String state,
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize){

        Integer status = NumberUtil.isInteger(state)?NumberUtil.parseInt(state):null;

        return userManager.findMovementByCondition4Page(uid,status,pageNum,pageSize);
    }

    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable String movementId){
        return userManager.findMovementById(movementId);
    }


    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize,
            String messageID){
        return userManager.findMovementComemntByPage(pageNum,pageSize,messageID);
    }

    @GetMapping("/manage/videos")
    public ResponseEntity findUserVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize,
            Long uid){
        return userManager.findUserVideoByPage(pageNum,pageSize,uid);
    }
/**
    //动态审核通过
    @PostMapping("/manage/messages/pass")
    public ResponseEntity means(
            @RequestBody List<String> items,
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){

        return userManager.means(pageNum,pageSize,items);
        // return ResponseEntity.ok(null);
    }

    //动态审核拒绝
    @PostMapping("/manage/messages/reject")
    public ResponseEntity refuse(
            @RequestBody List<String> items,
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){
        return userManager.refuse(pageNum,pageSize,items);
    }
*/
    //冻结
    @PostMapping("/manage/users/freeze")
    public ResponseEntity frozen(@RequestBody Map<String,String>map) {
        String userIds = map.get("userId");
        String freezingTimes = map.get("freezingTime");
        String freezingRange = map.get("freezingRange");
        String reasonsForFreezing = map.get("reasonsForFreezing");
        String frozenRemarks = map.get("frozenRemarks");

        int userId = Integer.parseInt(userIds);
        int freezingTime = Integer.parseInt(freezingTimes);
        return ResponseEntity.ok(userManager.frozen(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks));
    }
    @PostMapping("/manage/messages/pass")
    public ResponseEntity movementPass(@RequestBody String [] movementIds){
        return userManager.movementPass(movementIds);
    }

    @PostMapping("/manage/messages/reject")
    public ResponseEntity movementReject(@RequestBody String [] movementIds){
        return userManager.movementReject(movementIds);
    }

    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity thaw(@RequestBody Map<String,String>map){
        String userIds = map.get("userId");
        String reasonsForThawing = map.get("reasonsForThawing");

        Integer userId = Integer.parseInt(userIds);
        return ResponseEntity.ok(userManager.thaw(userId,reasonsForThawing));
    }


}
